#include "mywebtab.h"


MyWebTab::MyWebTab(MyWebTabMgr* pTabMgr, QWebEngineProfile *profile)
    :QFrame(pTabMgr->m_pMainDlg->ui->view_tab)
{
    m_pTabMgr = pTabMgr;
    m_pWebProfile = profile;
    if( m_pWebProfile == NULL ){
        m_pWebProfile = QWebEngineProfile::defaultProfile();
    }
    m_pLayout = new QVBoxLayout(this);

    m_pProcess = new QProgressBar(this);
    m_pProcess->setMaximumHeight(1);
    m_pProcess->setTextVisible(false);
    m_pProcess->setValue(0);
    m_pProcess->show();

    m_pWebView = new MyWebView(m_pTabMgr, this);
    m_pWebPage = new QWebEnginePage(m_pWebProfile, this);
    m_pWebView->setPage(m_pWebPage);
    m_pWebView->show();

    m_pLayout->setMargin(0);
    m_pLayout->setSpacing(0);
    m_pLayout->addWidget(m_pWebView);
    m_pLayout->addWidget(m_pProcess);

    this->setLayout(m_pLayout);
    m_pTabMgr->m_pMainDlg->ui->view_tab->addTab(this, QIcon(":/web.png"), "NewPage");

    connect(m_pWebPage, SIGNAL(iconChanged(QIcon)), this, SLOT(iconChanged(QIcon)));
    connect(m_pWebPage, SIGNAL(titleChanged(QString)), this, SLOT(titleChanged(QString)));
    connect(m_pWebPage, SIGNAL(loadProgress(int)), this, SLOT(loadProgress(int)));
    connect(m_pWebPage, SIGNAL(linkHovered(QString)), this, SLOT(notify_msg(QString)));
    connect(m_pWebPage, SIGNAL(urlChanged(QUrl)), this, SLOT(urlChanged(QUrl)));
    connect(m_pWebPage, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));

    QWebChannel* pWebChannel = new QWebChannel(m_pWebPage);
    pWebChannel->registerObject("sdk_down", m_pTabMgr->m_pDownLoadSDK);
    m_pWebPage->setWebChannel(pWebChannel);
}

void MyWebTab::setUrl(QString strUrl)
{
    m_pWebView->setUrl(QUrl(strUrl));
}

int MyWebTab::getTabIndex()
{
    return m_pTabMgr->m_pMainDlg->ui->view_tab->indexOf(this);
}

MyWebView *MyWebTab::getWebView()
{
    return m_pWebView;
}

QWebEngineProfile *MyWebTab::getWebProfile()
{
    return m_pWebProfile;
}

void MyWebTab::iconChanged(QIcon ico)
{
    int nIndex = m_pTabMgr->m_pMainDlg->ui->view_tab->indexOf(this);
    m_pTabMgr->m_pMainDlg->ui->view_tab->setTabIcon(nIndex, ico);
}

void MyWebTab::titleChanged(QString title)
{
    int nIndex = m_pTabMgr->m_pMainDlg->ui->view_tab->indexOf(this);
    m_pTabMgr->m_pMainDlg->ui->view_tab->setTabText(nIndex, title);
    m_pTabMgr->m_pMainDlg->ui->view_tab->setTabToolTip(nIndex, title);
}

void MyWebTab::notify_msg(QString msg)
{
    int nIndex = m_pTabMgr->m_pMainDlg->ui->view_tab->currentIndex();
    if( nIndex == m_pTabMgr->m_pMainDlg->ui->view_tab->indexOf(this)){
        emit m_pTabMgr->sig_notify_msg(msg);
    }
}

void MyWebTab::loadProgress(int nVal)
{
    m_pProcess->setValue(nVal);
    if( nVal > 99 ){
        m_pProcess->setValue(0);
    }
}

void MyWebTab::urlChanged(QUrl url)
{
    int nIndex = m_pTabMgr->m_pMainDlg->ui->view_tab->currentIndex();
    if( nIndex == m_pTabMgr->m_pMainDlg->ui->view_tab->indexOf(this)){
        emit m_pTabMgr->sig_url_msg(url.toString());
    }
}

void MyWebTab::loadFinished(bool bHasErr)
{
//    QString strUrl = m_pWebPage->url().toString().toLower();
//    if( strUrl.startsWith("file") ){
//        QWebChannel* pWebChannel = new QWebChannel(m_pWebPage);
//        pWebChannel->registerObject("sdk_down", m_pTabMgr->m_pDownLoadSDK);
//        m_pWebPage->setWebChannel(pWebChannel);
//    }
}


MyWebView::MyWebView(MyWebTabMgr *pTabMgr, QWidget* pParent)
    :QWebEngineView(pParent)
{
    m_pTabMgr = pTabMgr;
}

QWebEngineView *MyWebView::createWindow(QWebEnginePage::WebWindowType type)
{
    switch (type) {

        case QWebEnginePage::WebBrowserBackgroundTab: {

            return m_pTabMgr->CreateTab(NULL, false);
        }
        case QWebEnginePage::WebBrowserTab:
        case QWebEnginePage::WebBrowserWindow: {
            return m_pTabMgr->CreateTab(NULL, true);
        }
//            case QWebEnginePage::WebDialog: {
//                WebPopupWindow *popup = new WebPopupWindow(page()->profile());
//                return popup->view();
//            }
        default:{
            return m_pTabMgr->CreateTab(NULL, true);
        }
    }
    return this;
}
